package com.egao.orderB.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.PinyinUtils;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.DriverBiz;
import com.egao.orderB.entity.Driver;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2020-06-03 16:58:06
 * @version ：1.0
 */
@Repository
public class DriverBizImpl extends BaseDao implements DriverBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Driver driver, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id,d.car_team");
		sql.append(",d.phone,d.urgent_phone,d.name,d.id_card,d.id_card_pic,d.car_no,d.vehicle_license,d.vehicle_license_date,d.vehicle_license_date_start");
		sql.append(",d.drive_license,d.drive_license_date,d.drive_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.operation_license,d.operation_license_date,d.operation_license_date_start,d.qualification_certificate");
		sql.append(",d.type");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.white_card_pic,d.white_card_date,d.white_card_date_start,d.update_user");
		sql.append(",sognz.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join sys_organization sognz on sognz.organization_id=d.organization_id ");
		sql.append(" where d.deleted=0 and sognz.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(driver)){
			if(Utils.isNotEmpty(driver.getName())){
				sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin,:name)>0) ");
				map.put("name", driver.getName());
			}
			if(Utils.isNotEmpty(driver.getCarTeam())){
				sql.append(" and d.car_team=:car_team ");
				map.put("car_team", driver.getCarTeam());
			}
			if(Utils.isNotEmpty(driver.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driver.getCarNo());
			}
			if(Utils.isNotEmpty(driver.getWhiteCard())){
				sql.append(" and instr(d.white_card,:white_card)>0 ");
				map.put("white_card", driver.getWhiteCard());
			}
			if(Utils.isNotEmpty(driver.getState())){
				sql.append(" and instr(d.state,:state)>0 ");
				map.put("state", driver.getState());
			}
			if(Utils.isNotEmpty(driver.getPhone())){
				sql.append(" and instr(d.phone,:phone)>0 ");
				map.put("phone", driver.getPhone());
			}
			if(Utils.isNotEmpty(driver.getSort())&&Utils.isNotEmpty(driver.getOrder())){
				sql.append(" order by "+driver.getSort()+" "+driver.getOrder());
			}else {
				sql.append(" order by d.create_time desc");
			}
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Driver> list(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id,d.car_team");
		sql.append(",d.phone,d.urgent_phone,d.name,d.id_card,d.id_card_pic,d.car_no,d.vehicle_license,d.vehicle_license_date,d.vehicle_license_date_start");
		sql.append(",d.drive_license,d.drive_license_date,d.drive_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.operation_license,d.operation_license_date,d.operation_license_date_start,d.qualification_certificate");
		sql.append(",d.type");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.white_card_pic,d.white_card_date,d.white_card_date_start,d.update_user");
		sql.append(",sognz.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join sys_organization sognz on sognz.organization_id=d.organization_id ");
		sql.append(" where d.deleted=0 and sognz.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
			sql.append(" and d.organization_id=:tableOrganizationId ");
			map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
		}
		if(Utils.isNotEmpty(driver)){
			if(Utils.isNotEmpty(driver.getCarTeam())){
				sql.append(" and d.car_team=:car_team ");
				map.put("car_team", driver.getCarTeam());
			}
			if(Utils.isNotEmpty(driver.getName())){
				sql.append(" and (instr(d.name,:name)>0 or instr(d.pinyin,:name)>0) ");
				map.put("name", driver.getName());
			}
			if(Utils.isNotEmpty(driver.getCarNo())){
				sql.append(" and instr(d.car_no,:car_no)>0 ");
				map.put("car_no", driver.getCarNo());
			}
			if(Utils.isNotEmpty(driver.getState())){
				sql.append(" and instr(d.state,:state)>0 ");
				map.put("state", driver.getState());
			}
			if(Utils.isNotEmpty(driver.getWhiteCard())){
				sql.append(" and instr(d.white_card,:white_card)>0 ");
				map.put("white_card", driver.getWhiteCard());
			}
			if(Utils.isNotEmpty(driver.getPhone())){
				sql.append(" and instr(d.phone,:phone)>0 ");
				map.put("phone", driver.getPhone());
			}
			if(Utils.isNotEmpty(driver.getSort())&&Utils.isNotEmpty(driver.getOrder())){
				sql.append(" order by "+driver.getSort()+" "+driver.getOrder());
			}else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Driver getById(Driver driver) {
		RowMapper<Driver> rm = new BeanPropertyRowMapper<Driver>(Driver.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.car_team_id,d.car_team");
		sql.append(",d.phone,d.urgent_phone,d.name,d.id_card,d.id_card_pic,d.car_no,d.vehicle_license,d.vehicle_license_date,d.vehicle_license_date_start");
		sql.append(",d.drive_license,d.drive_license_date,d.drive_license_date_start,d.insurance_license,d.insurance_license_date,d.insurance_license_date_start");
		sql.append(",d.corporation_license_pic,d.bank_name,d.bank_no,d.state");
		sql.append(",d.operation_license,d.operation_license_date,d.operation_license_date_start,d.qualification_certificate");
		sql.append(",d.type");
		sql.append(",d.order_num,d.create_time,d.create_user,d.update_time");
		sql.append(",d.white_card,d.white_card_pic,d.white_card_date,d.white_card_date_start,d.update_user");
		sql.append(",sognz.organization_name organizationName,suser.username createName ");
		sql.append(" from chf_driver d ");
		sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
		sql.append(" inner join sys_organization sognz on sognz.organization_id=d.organization_id ");
		sql.append(" where d.deleted=0 and sognz.deleted=0 and d.id=:id ");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	/*@Override
	public boolean save(Driver driver, User loginUser) {
		driver.setPinyin(PinyinUtils.ToFirstChar(driver.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver (");
		sql.append("organization_id,phone,car_team_id,car_team,name,id_card,id_card_pic,car_no,white_card_pic,white_card,white_card_date");
		sql.append(",pinyin");
		sql.append(",vehicle_license,vehicle_license_date,drive_license,drive_license_date,corporation_license_pic,bank_name");
		sql.append(",operation_license,operation_license_date,qualification_certificate");
		sql.append(",type");
		sql.append(",insurance_license,insurance_license_date");
		sql.append(",bank_no,state,order_num,create_user) values ( ");
		sql.append(":organization_id,:phone,:car_team_id,:car_team,:name,:id_card,:id_card_pic,:car_no,:white_card_pic,:white_card,:white_card_date");
		sql.append(",:pinyin");
		sql.append(",:vehicle_license,:vehicle_license_date,:drive_license,:drive_license_date,:corporation_license_pic,:bank_name");
		sql.append(",:operation_license,:operation_license_date,:qualification_certificate");
		sql.append(",:type");
		sql.append(",:insurance_license,:insurance_license_date");
		sql.append(",:bank_no,:state,:order_num,:create_user)");
		map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("phone", driver.getPhone());
		map.put("car_team_id", driver.getCarTeamId());
		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("pinyin", driver.getPinyin());
		map.put("id_card", driver.getIdCard());
		map.put("id_card_pic", driver.getIdCardPic());
		map.put("car_no", driver.getCarNo());
		map.put("white_card_pic", driver.getWhiteCardPic());
		map.put("white_card", driver.getWhiteCard());
		map.put("white_card_date", driver.getWhiteCard());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("vehicle_license_date", driver.getVehicleLicenseDate());
		map.put("drive_license", driver.getDriveLicense());
		map.put("drive_license_date", driver.getDriveLicenseDate());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("operation_license", driver.getOperationLicense());
		map.put("operation_license_date", driver.getOperationLicenseDate());
		map.put("insurance_license", driver.getInsuranceLicense());
		map.put("insurance_license_date", driver.getInsuranceLicenseDate());
		map.put("qualification_certificate", driver.getQualificationCertificate());
		map.put("type", driver.getType());
		map.put("bank_no", driver.getBankNo());
		map.put("state", driver.getState());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
*/
	@Override
	public Integer apiSave(Driver driver, User loginUser) {
		driver.setPinyin(PinyinUtils.ToFirstChar(driver.getName()));
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into chf_driver (");
		sql.append("organization_id,phone,urgent_phone,car_team_id,car_team,name,id_card,id_card_pic,car_no,white_card_pic,white_card,white_card_date,white_card_date_start");
		sql.append(",pinyin");
		sql.append(",vehicle_license,vehicle_license_date,vehicle_license_date_start,drive_license,drive_license_date,drive_license_date_start,corporation_license_pic,bank_name");
		sql.append(",operation_license,operation_license_date,operation_license_date_start,qualification_certificate");
		sql.append(",type");
		sql.append(",insurance_license,insurance_license_date,insurance_license_date_start");
		sql.append(",bank_no,state,order_num,create_user) values ( ");
		sql.append(":organization_id,:phone,:urgent_phone,:car_team_id,:car_team,:name,:id_card,:id_card_pic,:car_no,:white_card_pic,:white_card,:white_card_date,:white_card_date_start");
		sql.append(",:pinyin");
		sql.append(",:vehicle_license,:vehicle_license_date,:vehicle_license_date_start,:drive_license,:drive_license_date,:drive_license_date_start,:corporation_license_pic,:bank_name");
		sql.append(",:operation_license,:operation_license_date,:operation_license_date_start,:qualification_certificate");
		sql.append(",:type");
		sql.append(",:insurance_license,:insurance_license_date,:insurance_license_date_start");
		sql.append(",:bank_no,:state,:order_num,:create_user)");
		map.put("organization_id", loginUser.getOrganizationId() + "");
		map.put("phone", driver.getPhone());
		map.put("urgent_phone", driver.getUrgentPhone());
		map.put("car_team_id", driver.getCarTeamId());
		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("pinyin", driver.getPinyin());
		map.put("id_card", driver.getIdCard());
		map.put("id_card_pic", driver.getIdCardPic());
		map.put("car_no", driver.getCarNo());
		map.put("white_card_pic", driver.getWhiteCardPic());
		map.put("white_card", driver.getWhiteCard());
		map.put("white_card_date", driver.getWhiteCardDate());
		map.put("white_card_date_start", driver.getWhiteCardDateStart());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("vehicle_license_date", driver.getVehicleLicenseDate());
		map.put("vehicle_license_date_start", driver.getVehicleLicenseDateStart());
		map.put("drive_license", driver.getDriveLicense());
		map.put("drive_license_date", driver.getDriveLicenseDate());
		map.put("drive_license_date_start", driver.getDriveLicenseDateStart());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("operation_license", driver.getOperationLicense());
		map.put("operation_license_date", driver.getOperationLicenseDate());
		map.put("operation_license_date_start", driver.getOperationLicenseDateStart());
		map.put("insurance_license", driver.getInsuranceLicense());
		map.put("insurance_license_date", driver.getInsuranceLicenseDate());
		map.put("insurance_license_date_start", driver.getInsuranceLicenseDateStart());
		map.put("qualification_certificate", driver.getQualificationCertificate());
		map.put("type", driver.getType());
		map.put("bank_no", driver.getBankNo());
		map.put("state", driver.getState());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		//		System.err.println("受到影响的行数=" + retVal + ",
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}
			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	/*
	* 这个方法不用的
	* */
	@Override
	public boolean updatePinYin(Driver driver, User loginUser) {
		driver.setPinyin(PinyinUtils.ToFirstChar(driver.getName()));
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("pinyin=:pinyin ");
		sql.append(" where id=:id ");
		map.put("pinyin", driver.getPinyin());
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}


	@Override
	public boolean update(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("car_team_id=:car_team_id,car_team=:car_team,phone=:phone,urgent_phone=:urgent_phone,id_card=:id_card,id_card_pic=:id_card_pic ");
		sql.append(",white_card_pic=:white_card_pic,white_card=:white_card,white_card_date=:white_card_date,white_card_date_start=:white_card_date_start ");
		sql.append(",name=:name,car_no=:car_no ");
		sql.append(",vehicle_license=:vehicle_license,vehicle_license_date=:vehicle_license_date,vehicle_license_date_start=:vehicle_license_date_start" +
				",drive_license=:drive_license,drive_license_date=:drive_license_date,drive_license_date_start=:drive_license_date_start" +
				",corporation_license_pic=:corporation_license_pic ");
		sql.append(",bank_name=:bank_name,bank_no=:bank_no");
		sql.append(",operation_license=:operation_license,operation_license_date=:operation_license_date,operation_license_date_start=:operation_license_date_start,qualification_certificate=:qualification_certificate");
		sql.append(",type=:type");
		sql.append(",insurance_license=:insurance_license,insurance_license_date=:insurance_license_date,insurance_license_date_start=:insurance_license_date_start");
		sql.append(",order_num=:order_num,update_user=:update_user where id=:id ");
		map.put("car_team_id", driver.getCarTeamId());
		map.put("car_team", driver.getCarTeam());
		map.put("phone", driver.getPhone());
		map.put("urgent_phone", driver.getUrgentPhone());
		map.put("white_card_pic", driver.getWhiteCardPic());
		map.put("white_card", driver.getWhiteCard());
		map.put("white_card_date", driver.getWhiteCardDate());
		map.put("white_card_date_start", driver.getWhiteCardDateStart());
		map.put("name", driver.getName());
		map.put("car_no", driver.getCarNo());
		map.put("id_card", driver.getIdCard());
		map.put("id_card_pic", driver.getIdCardPic());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("vehicle_license_date", driver.getVehicleLicenseDate());
		map.put("vehicle_license_date_start", driver.getVehicleLicenseDateStart());
		map.put("drive_license", driver.getDriveLicense());
		map.put("drive_license_date", driver.getDriveLicenseDate());
		map.put("drive_license_date_start", driver.getDriveLicenseDateStart());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
		map.put("operation_license", driver.getOperationLicense());
		map.put("operation_license_date", driver.getOperationLicenseDate());
		map.put("operation_license_date_start", driver.getOperationLicenseDateStart());
		map.put("insurance_license", driver.getInsuranceLicense());
		map.put("insurance_license_date", driver.getInsuranceLicenseDate());
		map.put("insurance_license_date_start", driver.getInsuranceLicenseDateStart());
		map.put("qualification_certificate", driver.getQualificationCertificate());
		map.put("type", driver.getType());
		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	/*@Override
	public boolean driverUpdate(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("phone=:phone,id_card=:id_card,id_card_pic=:id_card_pic ");
		sql.append(",white_card_pic=:white_card_pic,white_card=:white_card,white_card_date=:white_card_date ");
//		sql.append(",car_team=:car_team ");
		sql.append(",name=:name,car_no=:car_no ");
		sql.append(",vehicle_license=:vehicle_license,vehicle_license_date=:vehicle_license_date,drive_license=:drive_license,drive_license_date=:drive_license_date," +
				"corporation_license_pic=:corporation_license_pic ");
		sql.append(",bank_name=:bank_name,bank_no=:bank_no");
		sql.append(",operation_license=:operation_license,operation_license_date=:operation_license_date,qualification_certificate=:qualification_certificate");
		sql.append(",type=:type");
		sql.append(",insurance_license=:insurance_license,insurance_license_date=:insurance_license_date");
//		sql.append(",order_num=:order_num");
		sql.append(",update_user=:update_user where id=:id ");
		map.put("phone", driver.getPhone());
		map.put("white_card_pic", driver.getWhiteCardPic());
		map.put("white_card", driver.getWhiteCard());
		map.put("white_card_date", driver.getWhiteCardDate());
//		map.put("car_team", driver.getCarTeam());
		map.put("name", driver.getName());
		map.put("id_card", driver.getIdCard());
		map.put("id_card_pic", driver.getIdCardPic());
		map.put("car_no", driver.getCarNo());
		map.put("vehicle_license", driver.getVehicleLicense());
		map.put("vehicle_license_date", driver.getVehicleLicenseDate());
		map.put("drive_license", driver.getDriveLicense());
		map.put("drive_license_date", driver.getDriveLicenseDate());
		map.put("corporation_license_pic", driver.getCorporationLicensePic());
		map.put("bank_name", driver.getBankName());
		map.put("bank_no", driver.getBankNo());
		map.put("operation_license", driver.getOperationLicense());
		map.put("operation_license_date", driver.getOperationLicenseDate());
		map.put("insurance_license", driver.getInsuranceLicense());
		map.put("insurance_license_date", driver.getInsuranceLicenseDate());
		map.put("qualification_certificate", driver.getQualificationCertificate());
		map.put("type", driver.getType());
//		map.put("order_num", driver.getOrderNum()==null?null:driver.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}*/

	@Override
	public boolean remove(Driver driver, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(driver.getId().length()>0){
			for(String ID : driver.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update chf_driver set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}

	@Override
	public boolean removeReal(String id) {
		int count = 0;
		Map<String, String> map =  new HashMap<String, String>();;
		StringBuffer sql =new StringBuffer("delete from chf_driver "
				+ " where id =:id ");
		map.put("id", id);
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(),map);
		return count > 0;
	}

	@Override
	public boolean examine(Driver driver, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update chf_driver set ");
		sql.append("state=:state,update_user=:update_user where id=:id ");
		map.put("state", driver.getState());
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", driver.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}
}
                                                                                                                                                                                                         